/*
 * GameTile - Tiles for the tile-map
 *
 * Copyright 2008 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package rules;

class GameTile
{
	
	inline public static var ter_space = 0;
	inline public static var ter_ground = 1;
	inline public static var ter_energy = 2;

	inline public static var own_none = 0;
	inline public static var own_player = 1;
	
	inline public static var path_spaceonly = 1;
	inline public static var path_nonspace = 2;
	
	public var x : Int;
	public var y : Int;

	public var terrain : Int;
	public var ownership : Int;
	public var fort : FortressData;
	public var creeps : Array<Creep>;
	
	public var neighbors : Array<GameTile>;

	public var scratch : Dynamic; // scratchpad for temporary data
	
	inline public static var hexadjacentsodd = 
	[[-1,-1],[0,-1],[1,-1],
	 [-1,0], [0,1], [1,0]];

	inline public static var hexadjacentseven = 
	[[-1,0],[0,-1],[1,0],
	 [-1,1], [0,1], [1,1]];
	
	//         0,-1        2,-1
	//  -1,-1        1,-1
	//         0,0         2,0
	//  -1,0         1,0
	//         0,1         2,1
	//  -1,1         1,1
	//         0,2         2,2
	//
	//   ^-odd-^-even	
	
	
	public function new(x : Int, y : Int)
	{
		this.x = x;
		this.y = y;
		this.terrain = ter_space;
		this.terrain = own_none;
		this.creeps = new Array();
	}
	
	public function setneighbors(fullmap : Array<Array<GameTile>>)
	{
		neighbors = new Array();

		if (x/2==Std.int(x/2))
		for (n in hexadjacentseven)
		{
			if (GameMap.inbounds(x+n[0], y+n[1]))
			{
				var z = fullmap[x+n[0]][y+n[1]];
				neighbors.push(z);
			}
		}
		else
		for (n in hexadjacentsodd)
		{
			if (GameMap.inbounds(x+n[0], y+n[1]))
			{
				var z = fullmap[x+n[0]][y+n[1]];
				neighbors.push(z);
			}
		}		
		
	}
	
	public function groundnearby() : Bool
	{
		// true if there is ground in an adjacent tile
		// false otherwise
		
		
		return !(Lambda.foreach(neighbors, function(i : GameTile) 
			{return (i.terrain != ter_ground);} 
		));
	}

	public function spacenearby() : Bool
	{
		// true if there is space in an adjacent tile
		// false otherwise
		
		
		return !(Lambda.foreach(neighbors, function(i : GameTile) 
			{return (i.terrain != ter_space);} 
		));
	}

	public function fortnearby() : Bool
	{
		// true if there is a fort in an adjacent tile
		// false otherwise
		
		return !(Lambda.foreach(neighbors, function(i : GameTile) 
			{return (i.fort == null);} 
		));
	}

	public function noground() : Bool
	{
		// true if there are no ground tiles adjacent
		// false otherwise

		return (Lambda.foreach(neighbors, function(i : GameTile) 
			{return (i.terrain != ter_ground);} 
		));		
	}

	public function generatedistances(style : Int)
	{
				
		var scratcharr : Array<Node> = new Array();
		
		for (t in RulesAPI.g.m.iter)
		{
			new Node(-1,t);
		}
		
		this.scratch.value = 0;
		
		scratcharr.push(this.scratch);

		switch(style)
		{	
			case(path_spaceonly):
			while(scratcharr.length>0)
			{
				var cur : Node = scratcharr.shift();

				for (n in cur.parent.neighbors)
				{
					if ((n.scratch.value == -1) && 
						(n.terrain == ter_space))
					{
						scratcharr.push(n.scratch);
						n.scratch.value = cur.value + 1;					
					}
				}
			}
			case(path_nonspace):
			while(scratcharr.length>0)
			{
				var cur : Node = scratcharr.shift();

				for (n in cur.parent.neighbors)
				{
					if ((n.scratch.value == -1) && 
						(n.terrain != ter_space))
					{
						scratcharr.push(n.scratch);					
						n.scratch.value = cur.value + 1;					
					}
				}
			}
		}
		
		for (t in RulesAPI.g.m.iter)
		{
			t.scratch.gendir();
		}		
		
	}
	
}
